package thread;

public class Bank {
     String name;
     double money;

    public Bank(String name, double money) {
        this.name = name;
        this.money = money;
    }

    public static void main(String[] args) {
        Bank bank=new Bank("xh",150);
        new Thread(new DrawMoney(bank,100),"xh").start();
        new Thread(new DrawMoney(bank,70),"xm").start();
    }
}
class DrawMoney implements Runnable{
    double draw;
    Bank bank;
    public DrawMoney(Bank bank, double draw) {
        this.bank=bank;
        this.draw = draw;

    }

    @Override
    public  void run() {
        synchronized(bank){
            if (bank.money-draw<=0){
                System.out.println("钱不够了");
                return;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            bank.money=bank.money-draw;;
            System.out.println(Thread.currentThread().getName()+"取出"+draw+"还有"+bank.money);
        }


    }
}
